<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class FutureTweetsController extends AppController {

    public $helpers = array('Html', 'Form');

    /**
     * displays the view of the buffer
     *
     * @param: null
     *
     * @author "Mark Milad","Sherif Ismail"
     */
    function index() {
        $this->loadModel('User');
        $this->loadModel('Buffer');
        $this->loadModel('Day');
        $this->loadModel('Hour');
        $accountID = $this->Auth->User('current_account');
        $UserEmail = $this->Auth->User('email');
        $currentBuffer = $this->Buffer->find('all' , array('conditions' => array('account_id' => $this->Auth->User('current_account'))));
        $Buffer = $currentBuffer[0]['Buffer']['id'];
        $daysArray = (array) $this->Day->find('all' , array('conditions' => array('buffer_id' => $Buffer)));
        $hoursArray = (array) $this->Hour->find('all' , array('conditions' => array('buffer_id' => $Buffer)));
        $this->set('email', $UserEmail);
        $results[0] = $this->User->getPermission($accountID, $UserEmail);
        $results[1] = $this->FutureTweet->displayBuffer($accountID, $daysArray, $hoursArray);
        $results[1] = $this->FutureTweet->displayBuffer($accountID, $daysArray, $hoursArray);
        if ($this->request->data) {
                $edit = $this->request->data['FutureTweet']['Edit'];
                $editname = $this->request->data['FutureTweet']['Editname'];
                $this->add($edit, $editname);
                $results[1] = $this->FutureTweet->displayBuffer($accountID, $daysArray, $hoursArray);
            }
        $this->set('results', $results);
    }



    function view($curent_account= NULL){

       $this->loadModel('User');
       $image= ClassRegistry::init('Account')->find('first', array('conditions'=>array('id'=>$curent_account)));
        $this->set('image',$image);
        $accountID = $curent_account;
        $UserEmail = $this->Auth->User('email');
        $this->set('email', $UserEmail);
        $results[0] = $this->User->getPermission($accountID, $UserEmail);
        $results[1] = $this->FutureTweet->displayTimeline($accountID);
        if ($this->request->data) {
            if ($this->request->data['FutureTweet']['filter']) {
                if ($this->request->data['FutureTweet']['Filter by date'] != "") {
                    $date = $this->request->data['FutureTweet']['Filter by date'];
                    $datetime = explode('/', $date);
                    $current_time = $datetime[2] . '-' . $datetime[0] . '-' . $datetime[1];
                    $results[1] = $this->FutureTweet->displayTimeline($current_time);
                } else {
                    $this->Session->SetFlash('You did not choose a date !');
                }
            } else {
                $edit = $this->request->data['FutureTweet']['Edit'];
                $editname = $this->request->data['FutureTweet']['Editname'];
                $this->add($edit, $editname);
                $results[1] = $this->FutureTweet->displayTimeline($accountID);
            }
        }
        $this->set('results', $results);
    }

    function pending($curent_account=NULL){

        $this->loadModel('User');
        $image= ClassRegistry::init('Account')->find('first', array('conditions'=>array('id'=>$curent_account)));
        $this->set('image',$image);
        $accountID = $curent_account;
        $UserEmail = $this->Auth->User('email');
        $this->set('email', $UserEmail);
        $results[0] = $this->User->getPermission($accountID, $UserEmail);
        $results[1] = $this->FutureTweet->displayPending($accountID);
        if ($this->request->data) {
            if ($this->request->data['FutureTweet']['filter']) {
                if ($this->request->data['FutureTweet']['Filter by date'] != "") {
                    $date = $this->request->data['FutureTweet']['Filter by date'];
                    $datetime = explode('/', $date);
                    $current_time = $datetime[2] . '-' . $datetime[0] . '-' . $datetime[1];
                    $results[1] = $this->FutureTweet->displayFilteredPending($current_time);
                } else {
                    $this->Session->SetFlash('You did not choose a date !');
                }
            } else {
                $edit = $this->request->data['FutureTweet']['Edit'];
                $editname = $this->request->data['FutureTweet']['Editname'];
                $this->add($edit, $editname);
                $results[1] = $this->FutureTweet->displayPending($accountID);
            }
        }
        $this->set('results', $results);
    }


    /**
     * approves a post if its status is pending
     *
     * @param: int $id (the id of the posts to approve)
     *
     * @author "Mark Milad"
     */
    public function approvePost($id = NULL) {
        $this->loadModel('User');
        $email = $this->Auth->User('email');
        $accountID = $this->Auth->User('current_account');
        $permission = $this->User->getPermission($accountID, $email);
        $status = $this->FutureTweet->find('all', array('conditions' => array('FutureTweet.id' => $id)));
        if ($status[0]['FutureTweet']['status'] != 'pending') {
            $this->Session->setFlash('You can only approve pending posts');
            $this->Redirect(array('action' => 'index'));
        } else if($permission != 'moderator'){
            $this->Session->setFlash('Only moderators can approve posts');
            $this->Redirect(array('action' => 'index'));
        }
         else {
            $this->FutureTweet->approve($id);
            $this->Session->setFlash('Approved successfully ..');
            $this->Redirect(array('action' => 'index'));
        }
    }

    public function declinePost($id = NULL) {
        $this->loadModel('User');
        $email = $this->Auth->User('email');
        $accountID = $this->Auth->User('current_account');
        $permission = $this->User->getPermission($accountID, $email);
        $status = $this->FutureTweet->find('all', array('conditions' => array('FutureTweet.id' => $id)));
        if ($status[0]['FutureTweet']['status'] != 'pending') {
            $this->Session->setFlash('You can only decline pending posts');
            $this->Redirect(array('action' => 'index'));
        } else if($permission != 'moderator'){
            $this->Session->setFlash('Only moderators can decline posts');
            $this->Redirect(array('action' => 'index'));
        }
         else {
            $this->FutureTweet->decline($id);
            $this->Session->setFlash('Declined successfully ..');
            $this->Redirect(array('action' => 'index'));
        }
    }
    /**
     * responsible for adding and editing tweets.
     *
     * @param: $edit,$editname
     *
     * @author "sherif ismail"
     */
    function add($edit = null, $editname = null) {
        $this->loadModel('User');
        $accountID = $this->Auth->User('current_account');
        $useremail = $this->Auth->User('email');
        $permission = $this->User->getPermission($accountID, $useremail);
        if ($permission == '') {
            $this->Session->SetFlash('You are not authorized to view this account');
            return;
        }
        $status = 'pending';
        if ($permission == 'moderator') {
            $status = 'Approved';
        }
        if ($this->request->data) {
            $String_TweetText = "" . ($this->request->data['FutureTweet']['Text']);
            $check = True;
            if ($this->request->data['FutureTweet']['options'] == 1) {
                $current_time = date('Y-m-d H:i:s');
            } else {
                if ($this->request->data['FutureTweet']['options'] == 2) {
                    $current_time = $this->FutureTweet->addtobuffer($accountID);
                } else {
                    if ($this->request->data['FutureTweet']['Date'] != null and $this->request->data['FutureTweet']['hours'] != null and $this->request->data['FutureTweet']['minutes'] != null) {
                        $current_time = $this->addwithdate();
                        if ($current_time == false) {
                            $check = false;
                        }
                    } else {
                        $this->Session->SetFlash('Please complete the date and time inputs');
                        $check = false;
                    }
                }
            }
            if ($this->TweetChecker() && $check) {
                $this->request->data = $this->FutureTweet->update($current_time, $useremail, $String_TweetText, $accountID, $status);
                if ($edit) {
                    $this->Session->SetFlash('You are not the Owner of the tweet so you cant edit it.');
                    if ($editname==$useremail){
                    $this->FutureTweet->read(null, $edit);
                    $this->FutureTweet->set('release_date', $current_time);
                    $this->FutureTweet->set('tweet_text', $String_TweetText);
                    $this->FutureTweet->set('status', $status);
                    $this->FutureTweet->save();
                    $this->Session->SetFlash('Tweet edited successfully');
                    }
                } else {
                    $this->FutureTweet->save($this->request->data);
                    $this->Session->SetFlash('Tweet added successfully');
                }
            }
        }
    }

    /**
     * Adds a tweet with the specified date and time to the futuretweeets
     *
     * @param: null
     *
     * @author "sherif ismail"
     */
    function addwithdate() {

        $datetime = explode('/', $this->request->data['FutureTweet']['Date']);
        $current_time = $datetime[2] . '-' . $datetime[0] . '-' . $datetime[1] . ' ' . $this->request->data['FutureTweet']['hours'] . ':' . $this->request->data['FutureTweet']['minutes'] . ':00';
        if (strtotime(date('Y-m-d H:i:s')) > strtotime($current_time)) {
            $this->Session->SetFlash('You choosed a date in the past . Please choose a valid date .');
            $current_time = false;
        }
        return $current_time;
    }

    /**
     * Checks that the constraints on a tweet is fulfilled
     *
     * @param: null
     *
     * @author "sherif ismail"
     */
    function TweetChecker() {
        if ($this->request->data) {
            $String_TweetText = "" . ($this->request->data['FutureTweet']['Text']);
            if (strlen(trim(preg_replace('/\xc2\xa0/', ' ', $String_TweetText))) == 0) {
                $this->Session->SetFlash('Write something First');
            } else {
                if ($this->FutureTweet->overlength($String_TweetText)) {
                    return true;
                } else {
                    $this->Session->SetFlash('Your Tweet exceeded 140 chars');
                }
            }
        }
        return false;
    }

    /**
     * Description of deletTweet
     *
     * @param $tweetid
     *
     * deletes a tweet that not yet posted.
     *
     * @author Sherif Ismail
     */
    function deleteTweet($tweetID) {
        $email = $this->Auth->User('email');
        $result = $this->FutureTweet->deletetweet($email, $tweetID);
        $this->Session->SetFlash($result);
        $this->redirect(array('action' => 'index'));
    }


/**
   * description cron
   *to run the function postingTweet to post tweet on twitter
   *this function will be run through cron job
   *
   * @author Youssef Madkour
   */
  
   public function cron() {
        $this->FutureTweet->postingTweet();
        
    }


 public function approvePost1($id = NULL) {
        $this->loadModel('User');
        $email = $this->Auth->User('email');
        $accountID = $this->Auth->User('current_account');
        $permission = $this->User->getPermission($accountID, $email);
        $status = $this->FutureTweet->find('all', array('conditions' => array('FutureTweet.id' => $id)));
       
        if ($status[0]['FutureTweet']['status'] != 'pending') {
            $this->Session->setFlash('You can only approve pending posts');
            $this->Redirect(array('action' => 'pending',$status['0']['FutureTweet']['account_id']));
        } else if($permission != 'moderator'){
            $this->Session->setFlash('Only moderators can approve posts');
            $this->Redirect(array('action' => 'pending',$status['0']['FutureTweet']['account_id']));
        }
         else {
            $this->FutureTweet->approve($id);
            $this->Session->setFlash('Approved successfully ..');
            $this->Redirect(array('action' => 'pending',$status['0']['FutureTweet']['account_id']));
        }
    }

     public function rejectPost($id = NULL) {
        $this->loadModel('User');
        $email = $this->Auth->User('email');
        $accountID = $this->Auth->User('current_account');
        $permission = $this->User->getPermission($accountID, $email);
        $status = $this->FutureTweet->find('all', array('conditions' => array('FutureTweet.id' => $id)));
        if ($status[0]['FutureTweet']['status'] != 'pending') {
            $this->Session->setFlash('You can only decline pending posts');
            $this->Redirect(array('action' => 'pending',$status['0']['FutureTweet']['account_id']));
        } else if($permission != 'moderator'){
            $this->Session->setFlash('Only moderators can decline posts');
            $this->Redirect(array('action' => 'pending',$status['0']['FutureTweet']['account_id']));
        }
         else {
            $this->FutureTweet->decline($id);
            $this->Session->setFlash('Declined successfully ..');
            $this->Redirect(array('action' => 'pending',$status['0']['FutureTweet']['account_id']));
        }
    }




}